<?php

namespace App\Http\Controllers;

use App\Episode;
use App\Show;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Session;

class EpisodeController extends Controller
{


    public function __construct()
    {

        $this->middleware(['auth', 'admin'], ['except' => ['index',
            'show', 'talkShow', 'latesEpisodes',
            'recentlyUpdatedEpisodes', 'episodeByKorean',
            'episodeByThailand',
            'episodeByJapan',
            'ajaxYesterdayEpisodes',
            'ajaxPanEpisodes',
            'episodeByMovie', 'episodeByDocumentary',
            'episodeByCooked', 'episodeByBritain',
            'episodeByBaidduPan', 'episodeByTalkShow']]);
    }

    public function index()
    {
        $episodes = Episode::orderBy('created_at', 'desc')->paginate(50);

        $eps = Episode::where('movie_id', '>', 0)->where('show_id', '>', 0)->get();
        foreach ($eps as $ep) {
            $ep->show()->dissociate();
            $ep->show_id = null;
            $ep->save();
        }
        return view('episode.index', ['episodes' => $episodes]);
    }


    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show(Episode $episode)
    {
        return view('episode.show', compact('episode'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Episode $episode)
    {
        $shows = Show::lists('name_en', 'id');
        return view('episode.edit', compact('episode', 'shows'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Episode $episode)
    {
        $showIds = $request->input('show_list');
        if (count($showIds) >= 1) {
            $showId = $showIds[0];
            $episode->show_id = $showId;
        }
        $episode->update($request->all());
        $episode->save();

        Session::flash('success', '修改成功!');
        return redirect(url()->previous());
    }

    public function recentlyUpdatedEpisodes()
    {
        $todayEps = Cache::remember('home page today episode', 60 * 2, function () {
            return Episode::whereDate('created_at', '=', Carbon::today()->toDateString())
                ->orderBy('updated_at', 'desc')
                ->with('show', 'movie')->limit(500)
                ->get();
        });
        return view('episode.recentlyUpdated', compact('todayEps'));
    }

    public function episodeByTalkShow()
    {
        $key = 'EpisodeByTalkShow';
        $episodes = Cache::get($key);
        if (!$episodes) {
            $episodes = Episode::talkShow()->get();
            Cache::put('$key', $episodes, 60 * 6);
        }
        return view('episode.index', compact('episodes'));
    }

    public function episodeByBaidduPan()
    {
        $key = 'episodeByBaidduPan';
        $episodes = Cache::get($key);
        if (!$episodes) {
            $episodes = Episode::baiduPan()->get();
            Cache::put('$key', $episodes, 60);
        }
        return view('episode.index', compact('episodes'));
    }

    public function episodeByBritain()
    {
        $key = 'episodeByBritain';
        $episodes = Cache::get($key);
        if (!$episodes) {
            $episodes = Episode::britain()->get();
            Cache::put('$key', $episodes, 60 * 6);
        }
        return view('episode.index', compact('episodes'));
    }

    public function episodeByCooked()
    {
        $key = 'episodeByCooked';
        $episodes = Cache::get($key);
        if (!$episodes) {
            $episodes = Episode::cooked()->get();
            Cache::put('$key', $episodes, 60);
        }
        return view('episode.index', compact('episodes'));
    }

    public function episodeByDocumentary()
    {
        $key = 'episodeByDocumentary';
        $episodes = Cache::get($key);
        if (!$episodes) {
            $episodes = Episode::documentary()->get();
            Cache::put('$key', $episodes, 60 * 24);
        }
        return view('episode.index', compact('episodes'));
    }

    public function episodeByMovie()
    {
        $key = 'episodeByMovie';
        $episodes = Cache::get($key);
        if (!$episodes) {
            $episodes = Episode::movieEp()->get();
            Cache::put($key, $episodes, 60 * 6);
        }
        return view('episode.index', compact('episodes'));
    }

    public function episodeByJapan()
    {
        $key = 'episodeByJapan';
        $episodes = Cache::get($key);
        if (!$episodes) {
            $episodes = Episode::japan()->get();
            Cache::put('$key', $episodes, 60 * 24);
        }
        return view('episode.index', compact('episodes'));
    }

    public function episodeByThailand()
    {
        $key = 'episodeByThailand';
        $episodes = Cache::get($key);
        if (!$episodes) {
            $episodes = Episode::thailand()->get();
            Cache::put('$key', $episodes, 60 * 36);
        }
        return view('episode.index', compact('episodes'));
    }

    public function episodeByKorean()
    {
        $key = 'episodeByKorean';
        $episodes = Cache::get($key);
        if (!$episodes) {
            $episodes = Episode::korean()->get();
            Cache::put('$key', $episodes, 60 * 24);
        }
        return view('episode.index', compact('episodes'));
    }



}
